﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HUGATE.ItemObject;
using CoreLibs;
using HUGATE.Utility;

namespace HUGATE.Item.Controls.Category
{
    public partial class ParentCategory : System.Web.UI.UserControl
    {

        #region Variable
        public DataRow temp;
        //public DataTable dtbl;
        #endregion

        #region Support Function

        /// <summary>
        /// Load data in TreeView
        /// </summary>
        /// 

        private void FillTreeview(TreeNode tnParentdNode, int intCurrentID, DataTable dtbl)
        {

            if (tnParentdNode == null)
            {
                TVprcate.Nodes.Clear();
                tnParentdNode = new TreeNode();
                tnParentdNode.Text = "Root";
                tnParentdNode.Value = "0";
                TVprcate.Nodes.Add(tnParentdNode);

            }
            DataRow[] objRows = dtbl.Select("ParentCategoryID = " + intCurrentID.ToString());

            foreach (DataRow row in objRows)
            {
                TreeNode child = new TreeNode();
                child.Text = Convert.ToString(row["CategoryName"]);
                child.Value = Convert.ToString(row["CategoryID"]);
                tnParentdNode.ChildNodes.Add(child);
                FillTreeview(child, Convert.ToInt32(row["CategoryID"]), dtbl);
            }
        }

        #endregion

        #region Event Function
        protected void Page_Load(object sender, EventArgs e)
        {
            ITEMCategory objCategory = new ITEMCategory();
            DataTable dtb = objCategory.SearchData(string.Empty);
            rtvCategory.Nodes.Clear();
            BuildCategoryTreeWithRoot(rtvCategory, dtb, 0);
            rtvCategory.ExpandAllNodes();
        }
        private void BuildCategoryTreeWithRoot(Telerik.Web.UI.RadTreeView rtvCategoryInside, DataTable dtb, int parentID)
        {
            Telerik.Web.UI.RadTreeNode root = new Telerik.Web.UI.RadTreeNode("Danh Mục Hàng", "0");
            rtvCategoryInside.Nodes.Add(root);
            DataRow[] drws = dtb.Select("ParentCategoryID=" + parentID);
            foreach (DataRow drw in drws)
            {
                Telerik.Web.UI.RadTreeNode rtn = new Telerik.Web.UI.RadTreeNode(drw["RefCategoryID"].ToString() + " - " + drw["CategoryName"].ToString(), drw["CategoryID"].ToString());
                root.Nodes.Add(rtn);
                BuildCategoryNode(rtn, dtb, Convert.ToInt32(drw["CategoryID"]));
            }
        }

        private void BuildCategoryNode(Telerik.Web.UI.RadTreeNode parentNode, DataTable dtb, int parentID)
        {
            DataRow[] drws = dtb.Select("ParentCategoryID=" + parentID);
            foreach (DataRow drw in drws)
            {
                Telerik.Web.UI.RadTreeNode rtn = new Telerik.Web.UI.RadTreeNode(drw["RefCategoryID"].ToString() + " - " + drw["CategoryName"].ToString(), drw["CategoryID"].ToString());
                parentNode.Nodes.Add(rtn);
                BuildCategoryNode(rtn, dtb, Convert.ToInt32(drw["CategoryID"]));
            }
        }
        protected void rtvCategory_NodeClick(object sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
        {
            ITEMCategory itemcata = new ITEMCategory();
            DataTable dtbl = itemcata.SearchData("");

            if (e.Node.Value != "0")
            {
                temp = dtbl.Select("CategoryID = " + e.Node.Value)[0];
            }
            else
            {
                temp = null;
            }
            RaisePostBackGetStringEvent(e);
        }
        protected void TVprcate_SelectedNodeChanged(object sender, EventArgs e)
        {
            ITEMCategory itemcata = new ITEMCategory();
            DataTable dtbl = itemcata.SearchData("");

            if (TVprcate.SelectedValue != "0")
            {
                temp = dtbl.Select("CategoryID = " + TVprcate.SelectedValue)[0];
            }
            else
            {
                temp = null;
            }
            RaisePostBackGetStringEvent(e);
        }
        #endregion

        #region Event Handler
        public delegate void GetNameParentID(object sender, EventArgs e);
        public event GetNameParentID GetString = null;
        public void RaisePostBackGetStringEvent(EventArgs e)
        {
            if (GetString != null)
                GetString(temp, e);
        }
        #endregion


    }
}